package com.scalar.chiptrack.finance.actions;

import com.scalar.chiptrack.ChipTrackAction;
import com.scalar.chiptrack.finance.dao.UpdateViewCostDAO;
import com.scalar.chiptrack.finance.forms.ProductCostCodeSetupForm;
import com.scalar.chiptrack.operations.process.dao.NewProductDAO;
import com.scalar.chiptrack.users.User;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.commons.Constants;
import com.scalar.ScalarException;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.ArrayList;

/**
 * User: ganesh
 * Date: May 30, 2007
 * Time: 4:56:34 PM
 */
public class ProductCostCodeSetupAction extends ChipTrackAction
{
    private static final String ACTION_NAME = "/productCostCodeSetupAction.do";

    public ActionForward execute ( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response ) throws Exception
    {

       String action = request.getParameter( "action" );
       ProductCostCodeSetupForm productCostCodeSetupForm;
       HashMap info = new HashMap();
       HashMap errors = new HashMap();

       request.setAttribute ( SessionKeys.INFO_MESSAGES, info );
       request.setAttribute ( SessionKeys.ERROR_MESSAGES, errors );

       HttpSession session = request.getSession ();

       ArrayList partNumberList       = new ArrayList();
       ArrayList packageList          = new ArrayList();
       ArrayList pinList              = new ArrayList();
       ArrayList costCodeList         = new ArrayList();

       productCostCodeSetupForm =(ProductCostCodeSetupForm) form;
       User user = ( User ) session.getAttribute ( SessionKeys.USER_KEY );

       if ( user == null )
       {
           session.setAttribute ( SessionKeys.SOURCE_PAGE_KEY, ACTION_NAME + "?" + request.getQueryString () );
           return ( mapping.findForward ( "login" ) );
       }

        if ( "loadPartNumbers".equalsIgnoreCase( action ) || action == null  )
        {
            try
            {
                partNumberList = NewProductDAO.loadPartNumbers(null, 0, Constants.LOCATION_TYPE_FG, true , true, true, 0);
                packageList = UpdateViewCostDAO.loadPackages(null);
                pinList = UpdateViewCostDAO.loadPins(null);
            }
            catch ( ScalarException e )
            {
                e.printStackTrace();
                errors.put ( "Could not load Products", null );
            }
            request.setAttribute ( "partNumberList", partNumberList );
            request.setAttribute ( "packagesList", packageList );
            request.setAttribute ( "pinsList", pinList );
        }
        if ( "loadCostCodes".equalsIgnoreCase( action ) )
        {
            boolean success = false;
            try
            {
                productCostCodeSetupForm.setCostCodePartNumbersList(UpdateViewCostDAO.loadPartNumbers ( null, productCostCodeSetupForm.getProcessStepId(), productCostCodeSetupForm.getPackageType(), productCostCodeSetupForm.getPinType(), productCostCodeSetupForm.isWithoutCostCode() ) );
                if ( productCostCodeSetupForm.getCostCodePartNumbersList().size() == 0 || productCostCodeSetupForm.getCostCodePartNumbersList()==null )
                {
                    info.put ( "Could not fetch any results for the search criteria ", null );
                }
                costCodeList    = UpdateViewCostDAO.loadCostCodes(null);
                request.setAttribute ( "costCodeList", costCodeList );
                //productCostCodeSetupForm.setCostCodePartNumbersList(UpdateViewCostDAO.loadPartNumbers(null, productCostCodeSetupForm.getProcessStepId(), productCostCodeSetupForm.getPackageType(), productCostCodeSetupForm.getPinType()));
                partNumberList  = NewProductDAO.loadPartNumbers(null, 0, Constants.LOCATION_TYPE_FG, true , true, true, 0);
                packageList     = UpdateViewCostDAO.loadPackages(null);
                pinList         = UpdateViewCostDAO.loadPins(null);
                request.setAttribute ( "partNumberList", partNumberList );
                request.setAttribute ( "packagesList", packageList );
                request.setAttribute ( "pinsList", pinList );
                request.setAttribute ( "costCodeList", costCodeList );
            }
            catch ( ScalarException se )
            {
                se.printStackTrace();
                errors.put ("Could not fetch Cost Code Details ", null);
                success = false;
            }
        }
        if ( "saveCostCodes".equalsIgnoreCase( action ) )
        {
            boolean success = false;
            try
            {
                ArrayList costcodeSetupList= productCostCodeSetupForm.getCostCodePartNumbersList();
                if( costcodeSetupList.size() > 0 )
                {
                    UpdateViewCostDAO.saveCostCodeDetails(null, costcodeSetupList);
                    partNumberList  = NewProductDAO.loadPartNumbers(null, 0, Constants.LOCATION_TYPE_FG, true , true, true, 0);
                    packageList     = UpdateViewCostDAO.loadPackages(null);
                    pinList         = UpdateViewCostDAO.loadPins(null);
                    success=true;
                    if(success == true)
                    {
                        productCostCodeSetupForm.setBaseProduct( null );
                        productCostCodeSetupForm.setMarketingPartNumber( null );
                        productCostCodeSetupForm.setProcessStepId( 0 );
                        productCostCodeSetupForm.setPackageType( null );
                        productCostCodeSetupForm.setPinType( null );
                        productCostCodeSetupForm.setCostCodeId( null );
                        productCostCodeSetupForm.setCostCodePartNumbersList ( null );
                        productCostCodeSetupForm.setWithoutCostCode(false);
                        info.put( "Cost Codes updated successfully ", null );
                    }
                }
            }
            catch ( ScalarException se )
            {
                se.printStackTrace();
                errors.put ("Could not Update Cost Code Details ", null);
                success = false;
            }
            request.setAttribute ( "partNumberList", partNumberList );
            request.setAttribute ( "packagesList", packageList );
            request.setAttribute ( "pinsList", pinList );
       }
        return mapping.findForward("loadCostCodeDetials");
    }
}
